﻿// Inner Fire 游戏引擎库
// SkinAnimator - 骨骼动画
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 用于渲染项目中骨骼动画的计算与保存
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-03-22

#pragma once

#include <DirectXMath.h>
#include <EASTL/vector.h>
#include <graphics/graphics_def.h>
#include <memory>
#include <dx/core/frame_resource.h>
#include <dx/core/upload_buffer.h>

namespace ifire::dx {
class SkinAnimator {
public:
  SkinAnimator(int index) : index_(index), bone_count_(0) {}
  SkinAnimator(const SkinAnimator& rhs) = delete;
  SkinAnimator& operator=(const SkinAnimator& rhs) = delete;
  ~SkinAnimator() = default;

  // 进行一帧的更新
  void Update(UploadBuffer<SkinnedConstants>* buffer);

  inline int Index() const { return index_; }
  inline void SetRefresh() { dirty_count = FRAME_RESOURCE_COUNT; }
  inline size_t BoneCount() const { return bone_count_; }
  void SetBoneCount(size_t count) {
    bone_count_ = count;
    mats_.reserve(bone_count_);

    // 初始化骨骼矩阵
    for (size_t i = 0; i < bone_count_; i++) {
      mats_.push_back(IDENTITY_4X4);
    }
    SetRefresh();
  }

public:
  int dirty_count = FRAME_RESOURCE_COUNT;
  eastl::vector<DirectX::XMFLOAT4X4> mats_;

private:
  int index_;
  size_t bone_count_;
};

using SkinAnimatorPtr = std::unique_ptr<SkinAnimator>;
} // namespace ifire